package com.lhf.fvssystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhf.fvscommon.entity.SysPer;
import com.lhf.fvscommon.vo.SysPerOptionsVo;
import com.lhf.fvscommon.vo.SysPerTableVo;
import com.lhf.fvssystem.dao.SysPerDao;
import com.lhf.fvssystem.service.SysPerService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 权限表(SysPer)表服务实现类
 *
 * @author lhf
 * @since 2020-10-26 10:57:10
 */
@Service("sysPerService")
public class SysPerServiceImpl implements SysPerService {
    @Resource
    private SysPerDao sysPerDao;

    /**
     * 通过ID查询单条数据
     *
     * @param perId 主键
     * @return 实例对象
     */
    @Override
    public SysPer queryById(Integer perId) {
        return this.sysPerDao.selectById(perId);
    }


    /**
     * 新增数据
     *
     * @param sysPer 实例对象
     * @return 实例对象
     */
    @Override
    public SysPer insert(SysPer sysPer) {
        System.out.println(sysPer);
        this.sysPerDao.insert(sysPer);
        return sysPer;
    }

    /**
     * 修改数据
     *
     * @param sysPer 实例对象
     * @return 实例对象
     */
    @Override
    public SysPer update(SysPer sysPer) {
        this.sysPerDao.updateById(sysPer);
        return this.queryById(sysPer.getPerId());
    }

    /**
     * 通过主键删除数据
     *
     * @param perId 主键
     * @return 是否成功
     */
    @Override
    public int deleteById(Integer perId) {
        QueryWrapper<SysPer> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", perId);
        if (this.sysPerDao.selectCount(wrapper) > 0) {
            //不能删除
            return -1;
        } else {
            //执行删除
            return this.sysPerDao.deleteById(perId);
        }
    }

    /**
     * 条件查询权限
     *
     * @param perName
     * @return
     */
    @Override
    public Set<SysPerOptionsVo> selectAny(String perName) {
        return this.sysPerDao.selectMenu(perName, 0);
    }

    /**
     * 条件查询，并且分页
     *
     * @param per
     * @return
     */
    @Override
    public PageInfo<SysPerTableVo> selectByLimit(SysPer per, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);

//        if (per.isNull()) {
        per.setParentId(Optional.ofNullable(per.getParentId()).orElse(0));
//        }

        List<SysPerTableVo> vos = this.sysPerDao.selectByLimit(per);
        SysPer sp = new SysPer();
        BeanUtils.copyProperties(per, sp);

        for (SysPerTableVo vo : vos) {
            sp.setParentId(vo.getPerId());
            vo.setChildren(this.selectChildren(sp));
        }

        return PageInfo.of(vos);
    }

    /**
     * 查询子集
     *
     * @param per
     * @return
     */
    public List<SysPerTableVo> selectChildren(SysPer per) {

        List<SysPerTableVo> vos = this.sysPerDao.selectByLimit(per);

        for (SysPerTableVo vo : vos) {
            SysPer sp = new SysPer();
            BeanUtils.copyProperties(per, sp);
            sp.setParentId(vo.getPerId());
            vo.setChildren(this.selectChildren(sp));
        }
        return vos;
    }


    /**
     * 根据角色查询其所拥有的权限
     *
     * @param roleId
     * @return
     */
    @Override
    public List<SysPerOptionsVo> selectByRoleId(Integer roleId, Integer parentId) {
        List<SysPerOptionsVo> vos = this.sysPerDao.selectByRoleId(roleId, parentId);
        for (SysPerOptionsVo vo : vos) {
            vo.setChildren(this.selectByRoleId(roleId, vo.getValue()));
        }
        return vos;
    }

    /**
     * 查询角色没有的权限
     *
     * @param roleId
     * @return
     */
    @Override
    public List<SysPer> selectNotExitByRoleId(Integer roleId) {
        List<SysPer> vos = this.sysPerDao.selectNotExitByRoleId(roleId);
        List<SysPer> pers = this.sysPerDao.selectAll(0);

        Iterator<SysPer> vo = vos.iterator();
        Iterator<SysPer> sys = pers.iterator();

        while (sys.hasNext()) {
            SysPer next = sys.next();
            Integer id = next.getPerId();
            while (vo.hasNext()) {
                Integer perId = vo.next().getPerId();
                if (!id.equals(perId)) {
                    vos.remove(next);
                }
            }

        }





//        System.out.println(pers);


        return pers;
    }


}